/*
 * Copyright (c) 2019.  drakelee.base All rights reserved.
 */

package com.base.components.task.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * 定时器配置
 *
 * @author <a href="drakelee1221@gmail.com">LiGeng</a>
 * @version 1.0.0, 2017-08-03 14:41
 *
 */
@Configuration
@RefreshScope
public class QuartzConfiguration {
  @Value("${base.quartz.threadPool.corePoolSize}")
  private int corePoolSize;
  @Value("${base.quartz.threadPool.maxPoolSize}")
  private int maxPoolSize;
  @Value("${base.quartz.threadPool.queueCapacity}")
  private int queueCapacity;
  @Value("${base.quartz.threadPool.keepAliveMillisecond}")
  private int keepAliveMillisecond;

  @Bean
  public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(corePoolSize);
    executor.setMaxPoolSize(maxPoolSize);
    executor.setQueueCapacity(queueCapacity);
    executor.setKeepAliveSeconds(keepAliveMillisecond / 1000);
    return executor;
  }


  @Bean
  public SchedulerFactoryBean schedulerFactoryBean(ThreadPoolTaskExecutor taskExecutor){
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
    factory.setTaskExecutor(taskExecutor);
    factory.setWaitForJobsToCompleteOnShutdown(true);
    factory.setBeanName("scheduler");
    factory.setAutoStartup(false);
    return factory;
  }

}
